package com.wzzz.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

/*
 * 日期处理
 *
 */
public class SysDate {

	// 将长时间格式字符 转换为Date
	public static Date strToDateLong(String strDate) {
		if(strDate==null || strDate.length()<1){
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmm");
		java.text.ParsePosition pos = new java.text.ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	// 将长时间格式字符 转换为Date
	public static Date strToDate14Long(String strDate) {
		if(strDate==null || strDate.length()<1){
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		java.text.ParsePosition pos = new java.text.ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	// 字符串转时间
	public static Date strToDate(String strDate) {
		if(strDate==null || strDate.length()<1){
			return null;
		}
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		java.text.ParsePosition pos = new java.text.ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	// 将yyyyMMddHHmmss格式的date转换为字符串
	public static String dateToStr(Date date) {
		if(date==null){
			return "";
		}
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		String LgTime = sdformat.format(date);
		return LgTime;
	}
	// 将yyyyMMddHHmmss格式的date转换为字符串
	public static String date14ToStr(Date date) {
		if(date==null){
			return "";
		}
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
		String LgTime = sdformat.format(date);
		return LgTime;
	}
	// 将长时间格式包含汉字字符 转换为Date
	public static Date strchToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
		java.text.ParsePosition pos = new java.text.ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}
	// 将yyyy年MM月dd日 HH时mm分格式的date转换为字符串
	public static String datechToStr(Date date) {
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMddHHmm");
		String LgTime = sdformat.format(date);
		return LgTime;
	}
	// 将yyyyMMdd格式的date转换为字符串
	public static String dateToDateShort(Date date) {
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyyMMdd");
		String LgTime = sdformat.format(date);
		return LgTime;
	}

	// 取得日期yymm
	public static String getDateNY(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyMM");
		String temp = sdf.format(date);
		return temp;
	}

	// 取得今天日期yyyymmdd
	public static String getToday() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String temp = sdf.format(Calendar.getInstance().getTime());
		return temp;
	}
	// 取得今天日期yyyymmdd
	public static String getToday2() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String temp = sdf.format(Calendar.getInstance().getTime());
		return temp;
	}
	// 取得今天日期yyyy年mm月dd日
	public static String getReceiptToday() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		String temp = sdf.format(Calendar.getInstance().getTime());
		return temp;
	}

	// 取得昨天日期yyyymmdd
	public static String getYesterday() {
		Calendar ca = Calendar.getInstance();
		ca.roll(Calendar.DAY_OF_MONTH, false);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String temp = sdf.format(ca.getTime());
		return temp;
	}

	// 取得明天日期yyyymmdd
	public static String getTomorrow() {
		Calendar ca = Calendar.getInstance();
		ca.add(Calendar.DAY_OF_YEAR, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String temp = sdf.format(ca.getTime());
		return temp;
	}

	// 取得下一天的日期yyyymmdd
	public static String getNextDay(Date date) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(date);
		// ca.roll(Calendar.DAY_OF_MONTH, true);
		ca.add(Calendar.DAY_OF_YEAR, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String temp = sdf.format(ca.getTime());
		return temp;
	}

	// 取出当前系统时间
	public static String getNowHourSecond() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String temp = sdf.format(Calendar.getInstance().getTime());
		return temp;
		// 测试
		// return temp.substring(10,12);
	}

	// 检查启动时间是否是hh:mm:ss格式
	public static boolean checkTime(String time) {
		String regEx = "\\d{2}\\:\\d{2}\\:\\d{2}";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(time);
		return m.matches();
	}

	// 检查字符是符为null
	public static String checkStr(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	// 检查天数是否标准
	public static int changeDays(String days) {
		for (int i = 0; i < days.length(); i++) {
			char c = days.charAt(i);
			if (c < '0' || c > 9) {
				return 1;
			}
		}
		int day = Integer.parseInt(days);
		return (day > 365) ? 365 : day;
	}
	public static void main(String args[]){
		System.out.println(dateToStr(strToDateLong("201006211800")));
	}
}
